<?php

namespace app\common;

use think\Exception;
use Yansongda\Supports\Collection;

class Pay
{
	/**
	 * 支付
	 * @param $pay_type *支付类型:1=微信,2=支付宝
	 * @param  $pay_amount
	 * @param  $order_no
	 * @param $mark
	 * @param $attach
	 * @return Collection
	 * @throws Exception
	 */
	public static function pay($pay_type,$pay_amount,$order_no,$mark,$attach,$openid)
	{
		$config = config('pay');
		return match ($pay_type) {
			1 => \Yansongda\Pay\Pay::wechat($config)->mini([
				'out_trade_no' => $order_no,
				'description' => $mark,
				'amount' => [
					'total' => (int)bcmul($pay_amount, 100, 2),
					'currency' => 'CNY',
				],
				'payer' => [
					'openid' => $openid,
				],
				'attach' => $attach
			]),
			2 => \Yansongda\Pay\Pay::alipay($config)->mini([
				'out_trade_no' => $order_no,
				'total_amount' => $pay_amount,
				'subject' => $mark,
				'passback_params' => urlencode($attach)
			]),
			default => throw new Exception('支付类型错误'),
		};
	}
	
	#退款
	public static function refund($pay_type,$pay_amount,$order_no,$refund_order_no,$reason)
	{
		$config = config('pay');
		return match ($pay_type) {
			1 => \Yansongda\Pay\Pay::wechat($config)->refund([
				'out_trade_no' => $order_no,
				'out_refund_no' => $refund_order_no,
				'amount' => [
					'refund' => (int)bcmul($pay_amount, 100, 2),
					'total' => (int)bcmul($pay_amount, 100, 2),
					'currency' => 'CNY',
				],
				'reason'=>$reason
			]),
			default => throw new Exception('支付类型错误'),
		};
	}
	
}